package com.enterpriseDigitalPlatform.controller.renshi.dept;

import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.exception.ServiceException;
import com.enterpriseDigitalPlatform.util.RequiredCheckUtil;
import com.enterpriseDigitalPlatform.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/renshi-dept")
public class DeptC {
    private DeptDao deptDao;

    @Autowired
    public DeptC(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    @PostMapping("/addData")
    public Object addData(@RequestBody DeptDTO deptDTO) {
        if (deptDTO.getPid() == null) {
            deptDTO.setPid(0);
        }
        if (RequiredCheckUtil.checkString(deptDTO.getName())) {
            throw new ServiceException("部门名称不能为空");
        }
        if (RequiredCheckUtil.checkInt(deptDTO.getLevel())) {
            deptDTO.setLevel(3);
        }
        int row = deptDao.addData(deptDTO);
        return new JsonRes<>(row);
    }

    @PostMapping("/editData")
    public Object editData(@RequestBody DeptDTO deptDTO) {
        if (RequiredCheckUtil.checkInt(deptDTO.getId())) {
            throw new ServiceException("部门id不能为空");
        }
        if (RequiredCheckUtil.checkString(deptDTO.getName())) {
            throw new ServiceException("部门名称不能为空");
        }
        if (deptDTO.getPid() == null) {
            deptDTO.setPid(0);
        }
        if (RequiredCheckUtil.checkInt(deptDTO.getLevel())) {
            deptDTO.setLevel(3);
        }
        int row = deptDao.editData(deptDTO);
        return new JsonRes<>(row);
    }

    @PostMapping("/delData")
    public Object delData(@RequestBody DeptDTO deptDTO) {
        if (RequiredCheckUtil.checkInt(deptDTO.getId())) {
            throw new ServiceException("部门id不能为空");
        }
        int row = deptDao.delData(deptDTO);
        return new JsonRes<>(row);
    }

    @PostMapping("/getData")
    public Object getData(@RequestBody DeptDTO deptDTO) {
        if (RequiredCheckUtil.checkInt(deptDTO.getId())) {
            throw new ServiceException("部门id不能为空");
        }
        List<Dept> list = deptDao.getList(deptDTO);
        Dept dept = new Dept();
        if (!list.isEmpty()) {
            dept = list.get(0);
        }
        return new JsonRes<>(dept);
    }

    @PostMapping("/getTree")
    public Object getTree(@RequestBody DeptDTO deptDTO) {
        Integer pid = 0;
        if (deptDTO.getId() != null) {
            pid = deptDTO.getId();
        }
        List<Dept> list = deptDao.getList(new DeptDTO());
        Collection<Dept> treeList = TreeUtil.buildTree(list, pid);
        if (pid != 0) {
            DeptDTO select = new DeptDTO();
            select.setId(pid);
            List<Dept> list1 = deptDao.getList(select);
            if (!list1.isEmpty()) {
                Dept dept = list1.get(0);
                dept.setChildren(treeList);
                treeList = new ArrayList<>();
                treeList.add(dept);
            }
        }
        return new JsonRes<>(treeList);
    }
}
